﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace PTTPOS_Solution.DAL
{
    public class Print_DA
    {
        public static string findNameprinterbyID(string id)
        {
            
                string str = "";
                MySqlCommand com = new MySqlCommand("select * from tblprints where id=@id", Connection_PTTPOS.GetConnection());
                com.Parameters.AddWithValue("@id", id);
                com.Connection.Open();
                MySqlDataReader reader = com.ExecuteReader();
            try
            {
                if (reader.Read())
                {
                    str = reader["Printname"].ToString();
                }
                return str;
            }
            finally
            {
                com.Connection.Close();
            }
        }

        public static string findNameprinterbyName(string name)
        {

            string str = "";
            MySqlCommand com = new MySqlCommand("select * from tblprints where Printname=@name", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@name", name);
            com.Connection.Open();
            MySqlDataReader reader = com.ExecuteReader();
            try
            {
                if (reader.Read())
                {
                    str = reader["Printname"].ToString();
                }
                return str;
            }
            finally
            {
                com.Connection.Close();
            }
        }

        public static DataTable listPrinter = new DataTable();
        public static void getlistprint()
        {
            MySqlCommand com = new MySqlCommand("select * from tblprints", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter(com);
            listPrinter.Clear();
            adapter.Fill(listPrinter);
            com.Connection.Close();
        }

        public static void InsertPrinter(string ptrName)
        {
            getlistprint();
            if (listPrinter.Rows.Count > 0)
            {
                MySqlCommand com = new MySqlCommand("insert into tblprints(Name,Printname) values('',@ptrName)", Connection_PTTPOS.GetConnection());
                com.Parameters.AddWithValue("@ptrName", ptrName);
                com.Connection.Open();
                com.ExecuteNonQuery();
                com.Connection.Close();
            }
            else
            {
                MySqlCommand com = new MySqlCommand("insert into tblprints(id,Name,Printname) values(1,'',@ptrName)", Connection_PTTPOS.GetConnection());
                com.Parameters.AddWithValue("@ptrName", ptrName);
                com.Connection.Open();
                com.ExecuteNonQuery();
                com.Connection.Close();
            }
            
        }
        
        public static void UpdatePrinter(string ptrName)
        {
            UpdatePrinter2(ptrName);
                MySqlCommand com = new MySqlCommand("update tblprints set Printname=@ptrname where id=1", Connection_PTTPOS.GetConnection());
                com.Parameters.AddWithValue("@ptrName", ptrName);
                com.Connection.Open();
                com.ExecuteNonQuery();
                com.Connection.Close();
        }

        public static void UpdatePrinter2(string printname)
        {
            MySqlCommand com = new MySqlCommand("update tblprints set Printname=@ptrname1 where Printname=@printname2", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@ptrname1", findNameprinterbyID("1"));
            com.Parameters.AddWithValue("@printname2", printname);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }
    }
}
